function generateSpiralMatrix(n, m) {
    // 计算每行的数字个数
    const numbersPerRow = Math.ceil(n / m);
    // 创建一个二维数组用于存储螺旋矩阵
    const matrix = new Array(m).fill(null).map(() => new Array(numbersPerRow).fill('*'));
    let num = 1; // 当前填充的数字
    let top = 0; // 上边界
    let bottom = m - 1; // 下边界
    let left = 0; // 左边界
    let right = numbersPerRow - 1; // 右边界

    while (num <= n) {
        // 从左到右填充上边界
        for (let i = left; i <= right && num <= n; i++) {
            matrix[top][i] = num++;
        }
        top++;

        // 从上到下填充右边界
        for (let i = top; i <= bottom && num <= n; i++) {
            matrix[i][right] = num++;
        }
        right--;

        // 从右到左填充下边界
        for (let i = right; i >= left && num <= n; i--) {
            matrix[bottom][i] = num++;
        }
        bottom--;

        // 从下到上填充左边界
        for (let i = bottom; i >= top && num <= n; i--) {
            matrix[i][left] = num++;
        }
        left++;
    }

    return matrix;
}

function printMatrix(matrix) {
    console.log('matrix', matrix)

    for (let row of matrix) {
        console.log('row', row)

        console.log(row.join(' '));
    }
}

// 读取输入
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const matrix = generateSpiralMatrix(9, 4);
printMatrix(matrix);

// rl.question('请输入数字个数 n 和行数 m：', (answer) => {
//     const [n, m] = answer.split(' ').map(Number);
//     const matrix = generateSpiralMatrix(n, m);
//     printMatrix(matrix);
//     rl.close();
// });
